package com.fruit.service.user.impl;

import com.fruit.constants.RedisConstant;
import com.fruit.mapper.user.UserMapper;
import com.fruit.pojo.address.Address;
import com.fruit.pojo.sys.SysConfig;
import com.fruit.pojo.user.MyFruitTree;
import com.fruit.pojo.user.UserAccount;
import com.fruit.pojo.user.UserInfo;
import com.fruit.pojo.user.UserLogin;
import com.fruit.pojo.user.UserRelation;
import com.fruit.service.redis.UserRedisUtil;
import com.fruit.service.sms.SmsCodeManager;
import com.fruit.service.sys.SysManager;
import com.fruit.service.user.UserManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DateUtil;
import com.fruit.util.MD5Util;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;
import com.fruit.util.SerialCodeUtil;
import com.fruit.util.ValidateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 
 * @author admin
 * @version 2017.06.09 10:00
 * 
 */
@Service
public class UserService implements UserManager {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private SmsCodeManager smsCodeService;

	@Autowired
	private SysManager sysService;

	@Autowired
	private UserRedisUtil userRedisService;

	@Value("${app.key}")
	private String key;

	@Override
	public UserLogin getUserLoginByToken(Map<String, Object> params) {
		return userMapper.getUserLoginByToken(params);
	}

	@Override
	public UserInfo getUser(int userId) {
		UserInfo user = userMapper.getUserById(userId);
		if (user != null) {
			if (user.getSex() != null) {
				if ("00".equals(user.getSex())) {
					user.setSex("女");
				} else if ("01".equals(user.getSex())) {
					user.setSex("男");
				}else if ("02".equals(user.getSex())) {
					user.setSex("保密");
				}
			}
			if(user.getRealState()==null){
				user.setRealState("00");
			} else if("01".equals(user.getRealState())){
				user.setRealState("01");
			}else if("02".equals(user.getRealState())){
				user.setRealState("02");
			}else if("03".equals(user.getRealState())){				
				user.setRealState("03");
			}
		}
		return user;
	}

	@Override
	public UserInfo getUserInfoByLoginId(int loginId) {
		return userMapper.getUserInfoByLoginId(loginId);
	}

	@Override
	public List<Address> getAddressList(int userId) {
		return userMapper.getAddressList(userId);
	}

	@Override
	public void updateAddStatus(Address addre) {
		userMapper.updateAddStatus(addre);
	}

	@Override
	public void saveAddress(Address address) {
		userMapper.saveAddress(address);
	}

	@Override
	public void updateAddress(Address address) {
		userMapper.updateAddress(address);
	}

	@Override
	public Address getAddressById(int addressId) {
		return userMapper.getAddressById(addressId);
	}

	@Override
	public void deleteAddress(int addressId) {
		userMapper.deleteAddress(addressId);
	}

	@Override
	public void updateUserHeadImage(UserInfo user) {
		userMapper.updateUserHeadImage(user);
	}

	@Override
	public void updateUserMess(UserInfo user) {
		if (user.getPetName() != null && user.getPetName() != "") {
			userMapper.updateUserPetName(user);
			return;
		}
		if (user.getSex() != null && user.getSex() != "") {
			userMapper.updateUserSex(user);
		}
	}

	@Override
	public int updateUserLoginToken(UserLogin ul) {
		return userMapper.updateUserLoginToken(ul);
	}

	@Override
	public UserLogin getUserLoginByPhone(UserLogin ul) {
		return userMapper.getUserLoginByPhone(ul);
	}

	@Override
	public List<MyFruitTree> getMyFruitTreeList(Map<String, Object> parame) {
		List<MyFruitTree> mf = new ArrayList<MyFruitTree>();
		mf = userMapper.getMyFruitTreeList(parame);
		if (mf.size() > 0) {
			for (MyFruitTree mft : mf) {
				if ("00".equals(mft.getFruitTreeState())) {
					mft.setFruitTreeState("种子");
				} else if ("01".equals(mft.getFruitTreeState())) {
					mft.setFruitTreeState("树苗");
				} else if ("02".equals(mft.getFruitTreeState())) {
					mft.setFruitTreeState("果树");
				}

			}
		}
		return mf;
	}

	@Override
	public Map<String, Object> updateCodeToLogin(UserLogin ul,String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		UserInfo userInfo =null;
		try {
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-type", "参数有误!"));
				return resMap;
			}
			String msg = smsCodeService.getValidateCode(ul.getPhone(), ul.getSmsCode(), type);
			if (StringUtils.equals(msg, "expire")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_expire", "验证码已失效!"));
				return resMap;
			} else if (!StringUtils.equals(msg, "ok")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_error", "验证码输入错误或已失效!"));
				return resMap;
			}
			// 验证用户是否存在
			UserLogin userLogin = userMapper.getUserLoginByPhone(ul);
			if (userLogin != null) {
				userInfo = userMapper.getUserInfoByLoginId(userLogin.getId());
				if (!"01".equals(userInfo.getState())) {
					String msgs = "该用户已经被禁用，请联系管理员!";
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", msgs));
					return resMap;
				}

				ul.setId(userLogin.getId());
			}else{
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
				return resMap;
			}
			/*if (StringUtils.isEmpty(ul.getPwd())) {
				SmsCodeValidate validate = new SmsCodeValidate();
				validate.setCode(ul.getSmsCode());
				validate.setPhone(ul.getPhone());
				validate.setTypes(type);
				validate.setIsUse("01");
				smsCodeService.upudateIsSue(validate);
			}*/
			// 生成用户令牌
			SysConfig sc = sysService.getSysConfigList().get(0);
			int tokenTime = (int) (sc.getTokenTime() / 1000);
			String token = CustomUtil.makeUserToken(ul.getId(), key);
			userRedisService.setUserToken(RedisConstant.APP_USER_TOOKEN + userLogin.getId(), token, tokenTime);
			resMap.put("token", token);

			userInfo =getUser(userInfo.getId());
			resMap.put("userInfo", userInfo);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "登录成功!"));
			return resMap;
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result",
					ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		} finally {
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (ResultInfo.ROLLBACK.equals(ri.getIsRollBack())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
	}
	
	
	
	@Override
	public Map<String, Object> updatePwdToLogin(UserLogin ul) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		UserInfo userInfo=null;
		try {
			if(ul==null){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "账号或密码不能为空!"));
				return resMap;
			}
			if (StringUtils.isEmpty(ul.getPwd())||StringUtils.isEmpty(ul.getPhone())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "账号或密码不能为空!"));
				return resMap;
			}
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			String newPwd= MD5Util.MD5(ul.getPwd());
			// 验证用户是否存在
			UserLogin userLogin = userMapper.getUserLoginByPhone(ul);
			if (userLogin != null) {
				if (!StringUtils.isEmpty(ul.getPwd())) {
					if(!userLogin.getPwd().equalsIgnoreCase(newPwd)){
						resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "账号或密码输入错误"));
						return resMap;
					}
				}
				 userInfo = userMapper.getUserInfoByLoginId(userLogin.getId());
				if (!"01".equals(userInfo.getState())) {
					String msg = "该用户已经被禁用，请联系管理员!";
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", msg));
					return resMap;
				}

				ul.setId(userLogin.getId());
			}else{
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
				return resMap;
			}
			// 生成用户令牌
			SysConfig sc = sysService.getSysConfigList().get(0);
			int tokenTime = (int) (sc.getTokenTime() / 1000);
			String token = CustomUtil.makeUserToken(ul.getId(), key);
			userRedisService.setUserToken(RedisConstant.APP_USER_TOOKEN + userLogin.getId(), token, tokenTime);
			resMap.put("token", token);

			userInfo =getUser(userInfo.getId());
			resMap.put("userInfo", userInfo);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "登录成功!"));
			return resMap;
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result",
					ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		} finally {
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (ResultInfo.ROLLBACK.equals(ri.getIsRollBack())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
	}

	@Override
	public Map<String, Object> saveUser(UserLogin ul,String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		UserInfo user = new UserInfo();
		int num=1;
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			String msg = smsCodeService.getValidateCode(ul.getPhone(), ul.getSmsCode(), type);
			if (StringUtils.equals(msg, "expire")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_expire", "验证码已失效!"));
				return resMap;
			} else if (!StringUtils.equals(msg, "ok")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_error", "验证码输入错误或已失效!"));
				return resMap;
			}
			// 验证用户是否存在
			UserLogin userLogin = userMapper.getUserLoginByPhone(ul);
			if (userLogin != null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "is_exists", "您的手机号码已注册!"));
				return resMap;
			} else {
				UserLogin login=null;
				if(!StringUtils.isEmpty(ul.getParentName())){
					UserLogin ui=new UserLogin();
					ui.setPhone(ul.getParentName());
					login = userMapper.getUserByinviteCode(ui);
					if(login==null){
						resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no_exists", "邀请码未被识别,请仔细输入!"));
						return resMap;
					}
				}
				String newPwd= MD5Util.MD5(ul.getPwd());
				ul.setPwd(newPwd);
				num=userMapper.saveUserLoginPwd(ul);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				user.setCodePre(DateUtil.getYear());
				user.setCodeSuf(CustomUtil.buildRandom(3) + "");
				user.setLoginId(ul.getId());
				user.setPhone(ul.getPhone());
				String petName = CustomUtil.subPhone(user.getPhone());
				user.setPetName(petName);
				num=userMapper.saveUserInfo(user);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				String inviteCode=SerialCodeUtil.toSerialCode(user.getId());
				if(inviteCode==null || "".equals(inviteCode)){
					num=0;
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				user.setInviteCode(inviteCode);
				user.setVersion(0);
				num=userMapper.updateInviteCode(user);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				UserAccount account = new UserAccount();
				account.setUserId(user.getId());
				account.setLoginId(ul.getId());
				account.setBalance(new BigDecimal(0));
				account.setPoints(new BigDecimal(0));
				num=userMapper.saveUserAccount(account);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				if(!StringUtils.isEmpty(ul.getParentName())){
					UserRelation ur= new UserRelation();
					ur.setUserId(user.getId());
					ur.setParentId(login.getUserId());
					ur.setCreateTime(DateUtil.getTime());
					num=userMapper.saveUserRelation(ur);
					if(num==0){
						resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
						return resMap;
					}
				}
			}
		/*	SmsCodeValidate validate = new SmsCodeValidate();
			validate.setCode(ul.getSmsCode());
			validate.setPhone(ul.getPhone());
			validate.setTypes(type);
			validate.setIsUse("01");
			num=smsCodeService.upudateIsSue(validate);
			if(num==0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
				return resMap;
			}*/
			// 生成用户令牌
			SysConfig sc = sysService.getSysConfigList().get(0);
			int tokenTime = (int) (sc.getTokenTime() / 1000);
			String token = CustomUtil.makeUserToken(ul.getId(), key);
			userRedisService.setUserToken(RedisConstant.APP_USER_TOOKEN + ul.getId(), token, tokenTime);
			resMap.put("token", token);

			UserInfo userInfo = getUser(user.getId());
			resMap.put("userInfo", userInfo);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "注册成功"));
			return resMap;
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result",
					ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		} finally {
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (!ResultInfo.SUCCESS.equals(ri.getState())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
	}

	@Override
	public Map<String, Object> updateUserPwd(UserLogin ul, String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		if(StringUtils.isEmpty(type)){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
			return resMap;
		}
		String msg = smsCodeService.getValidateCode(ul.getPhone(), ul.getSmsCode(), type);
		if (StringUtils.equals(msg, "expire")) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_expire", "验证码已失效!"));
			return resMap;
		} else if (!StringUtils.equals(msg, "ok")) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_error", "验证码输入错误或已失效!"));
			return resMap;
		}
		UserLogin userLogin = userMapper.getUserLoginByPhone(ul);
		if(userLogin==null){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no_exists", "该手机号码尚未注册!"));
			return resMap;
		}
		String newPwd= MD5Util.MD5(ul.getPwd());
		ul.setPwd(newPwd);
		ul.setVersion(userLogin.getVersion());
		int num=userMapper.updatePwd(ul);
		if(num ==0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "修改失败!"));
			return resMap;
		}
		resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "修改成功!"));
		return resMap;
	}

	@Override
	public Map<String, Object> updatePwd(String userId, String oldPwd, String newPwd) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(oldPwd)||StringUtils.isEmpty(newPwd)) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数不全!"));
			return resMap;
		}
		oldPwd= MD5Util.MD5(oldPwd);
		UserLogin ul=userMapper.getUserLigonById(Integer.parseInt(userId));
		if(ul==null){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-user", "未找到用户!"));
			return resMap;
		}
		if(!ul.getPwd().equalsIgnoreCase(oldPwd)){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "旧密码输入错误!"));
			return resMap;
		}
		String pwd= MD5Util.MD5(newPwd);
		ul.setPwd(pwd);
		int num=userMapper.updatePwd(ul);
		if(num ==0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "修改失败!"));
			return resMap;
		}
		resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "修改成功!"));
		return resMap;
	}
	
	@Override
	public Map<String, Object> getUserParent(Integer userId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (userId==null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数不全!"));
				return resMap;
			}
			UserInfo ui=userMapper.getUserParent(userId);
			if(ui !=null){
				UserInfo u=userMapper.getUserParent(ui.getId());
				if(u !=null){
					if("01".equals(u.getBuyState())){
						resMap.put("parentTow", u);
					}
				}
				if("01".equals(ui.getBuyState())){
					resMap.put("parentOne", ui);
				}
			}else{
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "未找到上级用户!"));
				return resMap;
			}
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "获取成功!"));
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "网络繁忙!"));
		}
		return resMap;
	}

	@Override
	public Map<String, Object> getDownUserNum(Integer userId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		if (userId==null) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "no-param", "参数不全!"));
			return resMap;
		}
		try {
			int num=userMapper.getDownUserNum(userId);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "SUCCESS", "获取成功!"));
			resMap.put("downUser", num);
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "系统繁忙!"));
		}
		return resMap;
	}
	
	@Override
	public Map<String, Object> saveInviteUser(UserLogin ul, String type) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		UserInfo user = new UserInfo();
		int num=1;
		try {
			ResultInfo ri = ValidateUtil.phone(ul.getPhone());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			ri = ValidateUtil.smsCode(ul.getSmsCode());
			if (ri != null) {
				resMap.put("result", ri);
				return resMap;
			}
			if(StringUtils.isEmpty(type)){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "参数有误！"));
				return resMap;
			}
			String msg = smsCodeService.getValidateCode(ul.getPhone(), ul.getSmsCode(), type);
			if (StringUtils.equals(msg, "expire")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_expire", "验证码已失效!"));
				return resMap;
			} else if (!StringUtils.equals(msg, "ok")) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "code_error", "验证码输入错误或已失效!"));
				return resMap;
			}
			// 验证用户是否存在
			UserLogin userLogin = userMapper.getUserLoginByPhone(ul);
			if (userLogin != null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "is_exists", "您的手机号码已注册!"));
				return resMap;
			} else {
				String newPwd= MD5Util.MD5(ul.getPwd());
				ul.setPwd(newPwd);
				num=userMapper.saveUserLoginPwd(ul);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				user.setCodePre(DateUtil.getYear());
				user.setCodeSuf(CustomUtil.buildRandom(3) + "");
				user.setLoginId(ul.getId());
				user.setPhone(ul.getPhone());
				String petName = CustomUtil.subPhone(user.getPhone());
				user.setPetName(petName);
				num=userMapper.saveUserInfo(user);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				String inviteCode=SerialCodeUtil.toSerialCode(user.getId());
				if(inviteCode==null || "".equals(inviteCode)){
					num=0;
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				user.setInviteCode(inviteCode);
				user.setVersion(0);
				num=userMapper.updateInviteCode(user);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				UserAccount account = new UserAccount();
				account.setUserId(user.getId());
				account.setLoginId(ul.getId());
				account.setBalance(new BigDecimal(0));
				account.setPoints(new BigDecimal(0));
				num=userMapper.saveUserAccount(account);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
				UserRelation ur= new UserRelation();
				ur.setUserId(user.getId());
				ur.setParentId(ul.getUserId());
				ur.setCreateTime(DateUtil.getTime());
				num=userMapper.saveUserRelation(ur);
				if(num==0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
					return resMap;
				}
			}
			/*SmsCodeValidate validate = new SmsCodeValidate();
			validate.setCode(ul.getSmsCode());
			validate.setPhone(ul.getPhone());
			validate.setTypes(type);
			validate.setIsUse("01");
			num=smsCodeService.upudateIsSue(validate);
			if(num==0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "", "注册失败"));
				return resMap;
			}*/
			// 生成用户令牌
			SysConfig sc = sysService.getSysConfigList().get(0);
			int tokenTime = (int) (sc.getTokenTime() / 1000);
			String token = CustomUtil.makeUserToken(ul.getId(), key);
			userRedisService.setUserToken(RedisConstant.APP_USER_TOOKEN + ul.getId(), token, tokenTime);
			resMap.put("token", token);

			UserInfo userInfo = getUser(user.getId());
			resMap.put("userInfo", userInfo);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "注册成功"));
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result",ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		} finally {
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (!ResultInfo.SUCCESS.equals(ri.getState())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		return resMap;
	}

	@Override
	public Map<String, Object> updateUserBuyState(Integer userId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		int num=1;
		try {
			if(userId==null){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "NO-PARAMS", "参数错误"));
				return resMap;
			}
			UserInfo ui=userMapper.getUserById(userId);
			if(ui==null){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "用户不存在"));
				return resMap;
			}
			UserInfo u=new UserInfo();
			u.setId(userId);
			u.setVersion(ui.getVersion());
			num=userMapper.updateUserBuyState(u);
			if(num==0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "修改失败"));
				return resMap;
			}
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "修改成功"));
		} catch (Exception e) {
			num=0;
			e.printStackTrace();
			resMap.put("result",ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		}finally {
			if (num==0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		return resMap;
	}

	@Override
	public UserInfo getParentOne(Integer userId) {
		UserInfo ui= null;
		try {
			if(userId!=null){
				ui=userMapper.getUserParent(userId);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ui;
	}

}
